#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2025/1/21 9:25
# @Author : ZhengQL
import base64
import time
from playwright.sync_api import sync_playwright
import ddddocr
import pytest
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError

# @pytest.fixture(scope="session")
def cookie_login():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False, args=["--enable-logging", "--v=1"])
        context = browser.new_context(ignore_https_errors=True)
        page = context.new_page()
        page.goto("http://uatdxuas.wehdz.gov.cn:33698/#/login")
        time.sleep(2)
        # page.pause()

        page.get_by_placeholder("请输入账号或手机号").fill("ZhengQL")
        page.get_by_placeholder("请输入密码").fill("Zq1234@@")
        page.get_by_role("button", name="登录").click()
        # page.pause()
        time.sleep(1)

        # 拿到背景图
        img_src = page.query_selector(
            "#app > div > div > div > div.mask > div > div.verifybox-bottom > div > div.verify-img-out > div > img").get_attribute(
            'src')
        # print('验证码图片的 src:', img_src)

        # 转换成图片
        with open('bj_base.png', 'wb') as fp:
            bj_b = str(img_src).split(',')[-1]
            bj_base = base64.b64decode(bj_b)
            fp.write(bj_base)
        # 缺口图片
        img_src_Q = page.query_selector(
            "#app > div > div > div > div.mask > div > div.verifybox-bottom > div > div.verify-bar-area > div > div > div > img").get_attribute(
            'src')
        # print('二维码图片的 src:', img_src_Q)
        # 转换成图片
        with open('bj_base_a.png', 'wb') as fp:
            bj_b2 = str(img_src_Q).split(',')[-1]
            bj_base_a = base64.b64decode(bj_b2)
            fp.write(bj_base_a)

        # 计算滑动距离
        det = ddddocr.DdddOcr(det=False, ocr=False)
        ressta = det.slide_match(bj_base, bj_base_a, simple_target=True)["target"][0]
        # print("计算距离" + str(ressta))
        bili = round(400 / 315, 2)
        # print("图片比例" + str(bili))
        test = ressta * bili
        # print("可能的记录" + str(test))
        res = test + 30 + 3
        print("调整后的记录" + str(res))

        slide_btn = page.locator("#app > div > div > div > div.mask > div > div.verifybox-bottom > div > div.verify-bar-area > div > div")
        btn = slide_btn.bounding_box()
        print(btn)

        # 鼠标移动
        page.mouse.move(x=btn['x'], y=btn['y'] + 5)
        page.mouse.down()
        time.sleep(1)

        page.mouse.move(x=btn['x'] + res, y=btn['y'] + 5)
        page.mouse.up()

        # 等待验证完成
        time.sleep(2)
        print(page.title())

        # 等待到加载到指定页面,否则无法保存成功
        page.wait_for_url(url='http://uatdxuas.wehdz.gov.cn:33698/#/')
        # 保存登陆的cookies
        # print(f"项目的根目录：{pytestconfig.rootpath}")
        # storage_path = pytestconfig.rootpath.joinpath('auth/login111.json')
        # context.storage_state(path=storage_path)
        page.context.storage_state(path="D:\\11 PyCharm\ZQL\DG_WebTest/auth/login111.json")


         # 关闭
        time.sleep(4)
        browser.close()


if __name__ == '__main__':
    cookie_login()
